<?php
class RM_Module_Amenities extends RM_Module implements RM_Unit_Details_Tray_Interface, RM_Unit_List_Tray_Interface, RM_Search_Advanced_Interface {

    /**
     * This string is for rm_reservation_summary table
     */
    const SUMMARY_TYPE = 'amenities';

    public function  __construct()
    {
        $this->name = 'Amenities';
    }

    public function deleteLanguage($locale)
    {
        parent::deleteLanguage($locale);
        $model = new RM_Amenities();
        $model->deleteLanguage($locale);
    }

    public function addLanguage($locale)
    {
        parent::addLanguage($locale);
        $model = new RM_Amenities();
        $model->addLanguage($locale);
    }

    /**
     * This method will return node object for main admin menu tree.
     * Every child classes could overload this method to return any of the node object.
     * If there is no need to present a module in the main admin tree overloaded method should return NULL
     *
     * @return stdClass | null
     */
    public function getNode()
    {
        $std = new stdClass;

        $std->id = $this->name.'_ListJson_NoAjax';
        $std->text = $this->getName();

        $std->iconCls = 'RM_modules_leaf_icon';
        $std->leaf = 'true';
        return $std;
    }


	/**
	* get an array of the id's that match the search criteria
	*/
    public function getAdvancedSearchUnitIDs(RM_Unit_Search_Criteria $criteria){

        if (!$criteria->amenities || $criteria->amenities == ""){
            return false;
        }

        // just check if we acutally have any amenities selected...
        $amenitiesSet = false;

        $amenitySelection = explode(",",$criteria->amenities);

        $currentIso = RM_Environment::getInstance()->getLocale();

        $model = new RM_Amenities();

        $amenityIDs = array();

        // get all the unit id's this is for when the amenities are set to 0
        $unitsModel = new RM_Units();
        $units = $unitsModel->getAll(new RM_Unit_Search_Criteria())->toArray();
        foreach($units as $unit){
            $allUnitArray[] = $unit['id'];
        }

        $amenitysIDArray = array();

        // get the selected amenities and add to an array
        foreach($amenitySelection as $amenityid){

            $data = $model->getAmenitiesDetails($amenityid)->toArray();
            foreach($data as $item){
                if (isset($item['unit_id'])){
                    $amenitysIDArray[] = $item['unit_id'];
                }
            }
         }

        return $amenitysIDArray;
    }

    /**
     * Module Upgrade routine
     */
    public function upgrade(){

        $moduleViewFolderPath = implode(DIRECTORY_SEPARATOR, array(RM_Environment::getConnector()->getRootPath(),'RM','userdata','modules',$this->name,'views','user'));
        $mainFolderPath = implode(DIRECTORY_SEPARATOR, array(RM_Environment::getConnector()->getRootPath(),'RM','userdata','views','user','scripts'));

        // copy the search form panel into place
        $fileSource1 = implode(DIRECTORY_SEPARATOR, array($moduleViewFolderPath.DIRECTORY_SEPARATOR.'Search','Amenities_advancedsearch.phtml'));
        $fileDestin1 = implode(DIRECTORY_SEPARATOR, array($mainFolderPath.DIRECTORY_SEPARATOR.'Search'.DIRECTORY_SEPARATOR.'advanced','Amenities_advancedsearch.phtml'));
        $renameStatus1 = rename($fileSource1, $fileDestin1);

        // copy the unit details panel into place
        $fileSource2 = implode(DIRECTORY_SEPARATOR, array($moduleViewFolderPath.DIRECTORY_SEPARATOR.'Details','Amenities_amenities_details.phtml'));
        $fileDestin2 = implode(DIRECTORY_SEPARATOR, array($mainFolderPath.DIRECTORY_SEPARATOR.'Unit'.DIRECTORY_SEPARATOR.'details','Amenities_amenities_details.phtml'));
        $renameStatus2 = rename($fileSource2, $fileDestin2);

    }

    /**
    * Install routine
    */
    public function install() {
        parent::install();

        // userdata
        $moduleViewFolderPath = implode(DIRECTORY_SEPARATOR, array(
            RM_Environment::getConnector()->getRootPath(),
            'RM',
            'userdata',
            'modules',
            $this->name,
            'views',
            'user'

        ));

        $mainFolderPath = implode(DIRECTORY_SEPARATOR, array(
            RM_Environment::getConnector()->getRootPath(),
            'RM',
            'userdata',
            'views',
            'user',
            'scripts'
        ));

  		// copy the search form panel into place
        $fileSource1 = implode(DIRECTORY_SEPARATOR, array($moduleViewFolderPath.DIRECTORY_SEPARATOR.'Search','Amenities_advancedsearch.phtml'));
        $fileDestin1 = implode(DIRECTORY_SEPARATOR, array($mainFolderPath.DIRECTORY_SEPARATOR.'Search'.DIRECTORY_SEPARATOR.'advanced','Amenities_advancedsearch.phtml'));
        $renameStatus1 = rename($fileSource1, $fileDestin1);

        // copy the unit details panel into place
        $fileSource2 = implode(DIRECTORY_SEPARATOR, array($moduleViewFolderPath.DIRECTORY_SEPARATOR.'Details','Amenities_amenities_details.phtml'));
        $fileDestin2 = implode(DIRECTORY_SEPARATOR, array($mainFolderPath.DIRECTORY_SEPARATOR.'Unit'.DIRECTORY_SEPARATOR.'details','Amenities_amenities_details.phtml'));
        $renameStatus2 = rename($fileSource2, $fileDestin2);

        // copy the Amenities images into the images folder
        $imagesSource = implode(DIRECTORY_SEPARATOR, array(
            RM_Environment::getConnector()->getRootPath(),
            'RM',
            'userdata',
            'modules',
            $this->name,
            'images',
            'Amenities'
        ));
        $imagesDestination  = implode(DIRECTORY_SEPARATOR, array(
            RM_Environment::getConnector()->getRootPath(),
            'RM',
            'userdata',
            'images',
            'Amenities'
        ));
        $renameStatus3 = rename($imagesSource, $imagesDestination);

        // *********
        // Languages
        // *********
        // add any non-base languages that are installed to the module
        $languageModel = new RM_Languages();
        $languages = $languageModel->fetchAllEnabled(); // get all installed languages
        $baseLanguages = array("en_GB","fr_FR"); // exclude array
        $db = Zend_Db_Table::getDefaultAdapter();

        foreach ($languages as $language) {
            if (!in_array($language->iso, $baseLanguages)){
                $db->query("ALTER TABLE `rm_amenities` ADD `".$language->iso."` TEXT NOT NULL");
            }
        }
    }

    /**
     * uninstall routine
     */
    public function uninstall() {
        parent::uninstall();

        // remove the images/Amenities folder
        $file1 = implode(DIRECTORY_SEPARATOR, array(
            RM_Environment::getConnector()->getRootPath(),
            'RM',
            'userdata',
            'images',
            'Amenities'
        ));
        RM_Filesystem::deleteFile($file1);

        // remove the template files
        // unit details
        $file2 = implode(DIRECTORY_SEPARATOR, array(
            RM_Environment::getConnector()->getRootPath(),
            'RM',
            'userdata',
            'views',
            'user',
            'scripts',
            'Unit',
            'details',
            'Amenities_amenities_details.phtml'
        ));
        RM_Filesystem::deleteFile($file2);
        // search form
        $file3 = implode(DIRECTORY_SEPARATOR, array(
            RM_Environment::getConnector()->getRootPath(),
            'RM',
            'userdata',
            'views',
            'user',
            'scripts',
            'Search',
            'advanced',
            'Amenities_advancedsearch.phtml'
        ));
        RM_Filesystem::deleteFile($file3);
    }

    /**
     * This gets the HTML for the module try Icon
     *
     * @param RM_Unit_Row $unit
     * @return HTML
     */
    public function getHTML(RM_Unit_Row $unit){

        $configModel = new RM_AmenitiesConfig();
        $config = $configModel->fetchAll("id=1")->current()->toArray();

        if ($config['show_details'] === "1"){
            $icon = RM_Environment::getConnector()->getRootURL().'RM/userdata/modules/Amenities/images/tray_icon.png';
            return "<a href='javascript:void(0)' onclick=\"RM_doShadowBox('".RM_Environment::getInstance()->getRouter()->_('Amenities', 'showamenities', array("unit_id"=>$unit->id, "page" => "amenities.phtml"))."', '', 400, 200, 'iframe' )\" ><img src='".$icon."' border='0'></a>";
        }
        return false;
    }

    public function getListHTML(RM_Unit_Row $unit){

        $configModel = new RM_AmenitiesConfig();
        $config = $configModel->fetchAll("id=1")->current()->toArray();

        if ($config['show_list'] === "1"){
            $icon = RM_Environment::getConnector()->getRootURL().'RM/userdata/modules/Amenities/images/tray_icon.png';
            return "<a href='javascript:void(0)' onclick=\"RM_doShadowBox('".RM_Environment::getInstance()->getRouter()->_('Amenities', 'showamenities', array("unit_id"=>$unit->id, "page" => "amenities.phtml"))."', '', 400, 200, 'iframe' )\" ><img width='30px' height='30px' src='".$icon."' border='0'></a>";
        }
        return false;
    }
}